<!--
 * @Description: 前端测试页面
 * @Author: 梁萌
 * @Date: 2020-12-13 09:49:14
 * @LastEditTime: 2020-12-16 07:20:05
 * @FilePath: \koa_server\websocket_test.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket实例</title>
</head>

<body>
    <button id="connect">连接服务端</button>
    <button id="send" disabled="true">发送数据</button>
    <br> 从服务端接收的数据如下: <br>
    <span style="color:red" id="result"></span>

    <script>
        var connect = document.querySelector('#connect');
        var send = document.querySelector('#send');
        var result = document.querySelector('#result');

        let ws = null;
        connect.onclick = function() {
            ws = new WebSocket('ws://localhost:1111');
            ws.onopen = () => {
                console.log('连接服务器成功了');
                send.disabled = false;
            }
            ws.onclose = () => {
                console.log('连接服务器失败了');
                send.disabled = true;
            }
            ws.onmessage = (msg) => {
                console.log('接收到服务器发送过来的数据');
                result.innerHTML = msg.data;
            }
        }

        send.onclick = function() {
            ws.send(JSON.stringify({
                action: 'fullScreen',
                socketType: 'fullScreen',
                chartName: 'trend',
                value: true
            }));
        }
    </script>
</body>

</html>